import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "Home",
    component: () => import(/* webpackChunkName: 'Home' */ "../views/Home.vue"),
  },
];

//const loginUrl = `${process.env.VUE_APP_LOGIN_HOST}/login?clientUrl=${window.location.origin}/login`;

const router = new VueRouter({
  routes,
});

//挂载路由导航守卫
router.beforeEach((to, from, next) => {
  const localTicket = localStorage.getItem("ticket");

  if (!localTicket) {
    const url = new URL(location.href);
    const urlTicket = url.searchParams.get("ticket");
    if (urlTicket) {
      localStorage.setItem("ticket", urlTicket);
      return next();
    }
    // console.log(2222, "sso");
    location.href = `https://sso.yiche.com/login?clientUrl=${window.location.href}`;
  }
  next();
});

export default router;
